package com.iot.manage.controller;

import com.alibaba.fastjson.JSONObject;
import com.iot.manage.pojo.DeviceProperty;
import com.iot.manage.pojo.SysCode;
import com.iot.manage.result.Code;
import com.iot.manage.result.Result;
import com.iot.manage.service.DPropertyService;
import com.iot.manage.service.TokenService;
import com.iot.manage.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 设备属性管理控制层
 *
 * @author zhuzhi
 */
@RestController
@RequestMapping("/api/private/v1")
public class DPropertyController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private DPropertyService dpropertyService;

    /**
     * 查询设备属性信息列表
     *
     * @param token
     * @param query 模糊查询关键字
     * @param pagenum 当前页
     * @param pagesize 每页条数
     * @return
     */
    @GetMapping("/findDpropertyList")
    public Result findDpropertyList(
            @RequestHeader("token") String token,
            @RequestParam("query") String query,
            @RequestParam("pagenum") int pagenum,
            @RequestParam("pagesize") int pagesize) {
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        List<DeviceProperty> devicePropertyList;
        int devicePropertyTotal;
        try{
            devicePropertyList = dpropertyService.findDPropertyList(query,pagenum,pagesize);
            devicePropertyTotal = dpropertyService.countDPropertyList(query);
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total",devicePropertyTotal);
        map.put("pagenum",pagenum);
        map.put("devicePropertyList",devicePropertyList);
        return result.build(map, Code.S200);
    }

    /**
     * 新增设备属性信息
     *
     * @param token
     * @param params 设备属性信息参数
     * @return
     */
    @PostMapping("/addDevicePropertyInfo")
    public Result addDevicePropertyInfo(
            @RequestHeader("token") String token,
            @RequestBody JSONObject params) {
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        try {
            if(null != findDevicePropertyInfoByIdOrCode(token,params.getString("dpropertyCode")).getData()){
                return result.build(null, Code.D411);
            }
            params.put("dpropertyId", UUID.randomUUID().toString().replace("-", ""));
            params.put("dpropertyArg", StringUtils.replaceBlank(params.get("dpropertyArg").toString()));
            dpropertyService.addDevicePropertyInfo(params);
        } catch (Exception e) {
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(null, Code.S200);
    }

    /**
     * 根据设备属性ID或设备属性标识查询设备属性信息
     *
     * @param token
     * @param params 设备属性ID或设备属性标识
     * @return
     */
    @GetMapping("/findDevicePropertyInfoByIdOrCode/{params}")
    public Result findDevicePropertyInfoByIdOrCode(
            @RequestHeader("token") String token,
            @PathVariable("params") String params){
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        DeviceProperty deviceProperty;
        try{
            deviceProperty = dpropertyService.findDevicePropertyInfoByIdOrCode(params);
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(deviceProperty, Code.S200);
    }

    /**
     * 修改设备属性信息
     *
     * @param token
     * @param params 设备属性信息参数
     * @return
     */
    @PutMapping("/updateDevicePropertyInfo/{oldDevicePropertyCode}")
    public Result updateDevicePropertyInfo(
            @RequestHeader("token") String token,
            @PathVariable("oldDevicePropertyCode") String oldDevicePropertyCode,
            @RequestBody JSONObject params){
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        try {
            if (!(oldDevicePropertyCode.equals(params.get("dpropertyCode")))){
                if(null != findDevicePropertyInfoByIdOrCode(token,params.getString("dpropertyCode")).getData()){
                    return result.build(null, Code.D411);
                }
            }
            params.put("dpropertyArg", StringUtils.replaceBlank(params.get("dpropertyArg").toString()));
            dpropertyService.updateDevicePropertyInfo(params);
            params.put("oldDevicePropertyCode",oldDevicePropertyCode);
            dpropertyService.updateDeviceModelArg(params);
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(null, Code.S200);
    }

    /**
     *删除设备属性信息
     *
     * @param token
     * @param dpropertyId 设备属性ID
     * @return
     */
    @DeleteMapping("/deleteDPropertyInfo/{dpropertyId}/{propertyCode}")
    public Result deleteDPropertyInfo(
            @RequestHeader("token") String token,
            @PathVariable("dpropertyId") String dpropertyId,
            @PathVariable("propertyCode") String propertyCode) {
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        try {
            int total = dpropertyService.countPropertyUseTotal(propertyCode);
            if (total > 0){
                return result.build(null,Code.WARM);
            }
            dpropertyService.deleteDPropertyInfo(dpropertyId);
        } catch (Exception e) {
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(null, Code.S200);
    }

    /**
     * 查询属性类型下拉框
     *
     * @param token
     * @return
     */
    @GetMapping("/findDPropertyTypeList")
    public Result findDPropertyTypeList(@RequestHeader("token") String token){
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        List<SysCode> dpropertyTypeList;
        try{
            dpropertyTypeList = dpropertyService.findDPropertyTypeList();
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(dpropertyTypeList, Code.S200);
    }

    /**
     * 查询属性单位下拉框
     *
     * @param token
     * @return
     */
    @GetMapping("/findDPropertyUnitList")
    public Result findDPropertyUnitList(@RequestHeader("token") String token){
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        List<SysCode> dpropertyUnitList;
        try{
            dpropertyUnitList = dpropertyService.findDPropertyUnitList();
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(dpropertyUnitList, Code.S200);
    }
}
